<?

/**********************************************************************************************************************/
/**********************************************************************************************************************/
/*************************************         CONF            ********************************************************/
/**********************************************************************************************************************/
/**********************************************************************************************************************/

function list_clusters_conf() {
?>
<center>PostgreSQL clusters</center></br><br>
<table border=0 align=center>
    <tr bgcolor=#CCCCCC>
	<td><b>Hostname</b></td>
	<td><b>IP Address</b></td>
	<td><b>F.Q.D.N.</b></td>
	<td><b>DB Role</b></td>
	<td><b>Pg Version</b></td>
	<td><b>Data Directory</b></td>
	<td><b>Comments</b></td>
	<td><b>Observable</b></td>
	<td><b>Edit</b></td>
	<td><b>Delete</b></td>
    </tr>
<?
//    $hosts=new Hosts();

    $sql=new PgMonSQL();
    $sql->select_c("SELECT id FROM host_cluster WHERE alive ORDER BY hostname");

    foreach ($sql->get_result() as $hc) {
	$host=new WUHostCluster($hc['id']);
	echo $host->print_config_table_row();
    }


    echo "</table><br><center><a href=".$_SERVER['PHP_SELF']."?action=add&content=host_cluster>Add new cluster</a><br></center>";
}

/**********************************************************************************************************************/

function list_cluster_databases($hc_id,$change=array()) {
    $hc=new HostCluster($hc_id);
?>
<!--
<p style="background-color: #EEE">Databases on cluster <b><?=$hc->get_field('hostname')?></b></p>
-->
<div style="background-color: #DDD">Databases on cluster <b><?=$hc->get_field('hostname')?></b></div>
<table border=0 align=center>
    <tr bgcolor=#DDD>
	<td><b>Database</b></td>
	<td><b>Description</b></td>
	<td><b>Observable</b></td>
	<td><b>Action</b></td>
    </tr>
<?
    $hc->initiate_composit('database_name',array('alive'=>'t'));

    $dbs=$hc->get_composit('database_name',COLLECTION_OBJECT_ARRAY);
//    print_r($dbs);

    sort($dbs,SORT_STRING);
    if (isset($change['added'])) {
	foreach ($dbs as $db) {
	    echo $db->print_config_table_row(array_values($change['added']));
	}
    } else {
	foreach ($dbs as $db) {
	    echo $db->print_config_table_row(array());
	}
    }

    echo "</table><center>";

    if (isset($change['removed'])) {
	echo "<br>Removed databases:<br> ";
	foreach ($change['removed'] as $removed) {
	    echo "<b><i>".$removed."</i></b><br>";
	}
    }
    echo "<br><br><a href=".$_SERVER['PHP_SELF']."?level=0&action=discover&content=host_cluster&hc_id=".$hc_id.">Discover</center><br>";
}


/**********************************************************************************************************************/

function list_database_schemas($dn_id,$change=array()) {
    $dn=new DatabaseName($dn_id);
    $hc=new HostCluster($dn->get_field('hc_id'));
?>
Schemas for database <b><?=$dn->get_field('db_name')?></b> on cluster <b><?=$hc->get_field('hostname')?></b><br>

<table border=0 align=center>
    <tr bgcolor=#CCCCCC align=center>
	<td><b>Schema</b></td>
	<td><b>Description</b></td>
	<td><b>Observable</b></td>
	<td><b>Action</b></td>
	<td><b>Tables</b></td>
	<td><b>Indexes</b></td>
    </tr>
<?
    $dn->initiate_composit('schema_name',array('alive'=>'t'));
    $sns=$dn->get_composit('schema_name');
    sort($sns,SORT_STRING);
    if (isset($change['added'])) {
	foreach ($sns as $sn) {
	    echo $sn->print_config_table_row(array_values($change['added']));
	}
    } else {
	foreach ($sns as $sn) {
	    echo $sn->print_config_table_row(array());
	}

    }

    echo "</table><center>";

    if (isset($change['removed'])) {
	echo "<br>Removed schemas:<br> ";
	foreach ($change['removed'] as $removed) {
	    echo "<b><i>".$removed."</i></b><br>";
	}
    }

    echo "<br><br><center><a href=".$_SERVER['PHP_SELF']."?action=discover&content=database_name&dn_id=".$dn_id.">Discover</center><br>";
}

/**********************************************************************************************************************/
/**********************************************************************************************************************/
/*************************************         STAT            ********************************************************/
/**********************************************************************************************************************/
/**********************************************************************************************************************/
function list_clusters_stat() {
    echo "<table border=0 align=center>";
    $stat_types=array('Index-Seq Scans','Idx Tup Fetch','Hot updates','Modify Factors');
    $time_ranges=array('3' => 'Last 3 hours','6'=> 'Last 6 Hours','12' => 'Last 12 hours','24'=>'Last day','168'=>'Last week','672'=>'Last month');


    $sql=new PgMonSQL();
    $sql->select_c("SELECT id,hostname FROM host_cluster WHERE alive ORDER BY hostname");
    foreach ($sql->get_result() as $hc) {
	$hc=new HostCluster($hc['id']);
	echo "<tr>
	<td><b>".$hc."</b></td>
	<td><select name=ts_database>";
	foreach ($hc->get_composit_array() as $dn) {
	    if ($dn->get_field('observable') == 't') {
		echo "<option value=".$dn->get_id().">".$dn."</option>";
	    }
	}
/*
	echo "</td><td><select name=ts_stat_type>";
	foreach ($stat_types as $st) {
	    echo "<option>".$st;
	}
	echo "</td><td><select name=ts_time_range>";
	foreach ($time_ranges as $tr=>$name) {
	    echo "<option value=".$tr.">".$name."</option>";
	}
*/
	echo "</td><td><input type=submit name=ts_submit value=Submit></td>
	</tr>";
    }
}

function list_databases_stat($hc_id) {
    foreach ($hc->get_composit_array() as $dn) {
	if ($dn->get_field('observable') == 't') {
	    echo $dn."<br>";
	}
    }
}
?>
